﻿using System;
using System.Windows.Input;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using MetroGTD.Model;

namespace MetroGTD.ViewModel
{
    public interface IAddTaskViewModel
    {
        /// <summary>
        /// Gets or sets the title.
        /// </summary>
        /// <value>
        /// The title.
        /// </value>
        string Title { get; set; }

        /// <summary>
        /// Gets or sets the details.
        /// </summary>
        /// <value>
        /// The details.
        /// </value>
        string Details { get; set; }

        /// <summary>
        /// Gets the save command.
        /// </summary>
        ICommand Save { get; }
    }

    /// <summary>
    /// View model class to add a thing.
    /// </summary>
    public class AddTaskViewModel : ViewModelBase, IAddTaskViewModel
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="AddTaskViewModel"/> class.
        /// </summary>
        /// <param name="newThing">The new thing.</param>
        /// <param name="context">The context.</param>
        public AddTaskViewModel(Thing newThing, IThingsDataContext context)
        {
            if (newThing == null)
            {
                throw new ArgumentNullException("newThing");
            }
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }

            _newThing = newThing;
            _context = context;
        }

        /// <summary>
        /// Gets or sets the title.
        /// </summary>
        /// <value>
        /// The title.
        /// </value>
        public string Title
        {
            get { return _newThing.Title; }
            set
            {
                _newThing.Title = value;
                RaisePropertyChanged("Title");
            }
        }

        /// <summary>
        /// Gets or sets the details.
        /// </summary>
        /// <value>
        /// The details.
        /// </value>
        public string Details
        {
            get { return _newThing.Details; }
            set
            {
                _newThing.Details = value;
                RaisePropertyChanged("Details");
            }
        }

        /// <summary>
        /// Gets the save command.
        /// </summary>
        public ICommand Save
        {
            get
            {
                if (_save == null)
                {
                    _save = new RelayCommand(SaveThing, CanSaveThing);
                }
                return _save;
            }
        }

        bool CanSaveThing()
        {
            return !string.IsNullOrWhiteSpace(_newThing.Title);
        }

        void SaveThing()
        {
            _context.SaveThing(_newThing);
        }

        readonly Thing _newThing;
        readonly IThingsDataContext _context;
        ICommand _save;    
    }
}
